package com.sl.springboot.service.impl;

import com.sl.springboot.dao.AdminDao;
import com.sl.springboot.dao.CoachDao;
import com.sl.springboot.model.Admin;
import com.sl.springboot.model.Coach;
import com.sl.springboot.model.PageInfo;
import com.sl.springboot.service.AdminService;
import com.sl.springboot.service.CoachService;
import com.sl.springboot.util.RandomIdUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 用户Service接口实现类
 * @author: mty
 */
@Service("coachService")
@Transactional
public class CoachServiceImpl implements CoachService {

    @Autowired
    private CoachDao coachDao;


    //分页查询
    @Override
    public PageInfo<Coach> findPageInfo(Integer pageIndex, Integer pageSize, Map mp) {
        PageInfo<Coach> pi = new PageInfo<Coach>();
        if(!Objects.isNull(pageIndex)){
            pi.setPageIndex(pageIndex);
            pi.setPageSize(pageSize);
            mp.put("currentPage",(pi.getPageIndex()-1)*pi.getPageSize());
            mp.put("pageSize",pi.getPageSize());
        }
        //获取总条数
        Integer totalCount = coachDao.totalCount(mp);
        if (totalCount>0){
            pi.setTotalCount(totalCount);
            //按条件查询
            List<Coach> adminList =	coachDao.getCoachList(mp);
            pi.setList(adminList);
        }
        return pi;
    }


    //添加
    @Override
    public int addCoach(Coach coach) {
        coach.setId(RandomIdUtil.getRandomIdByUUID());
        return coachDao.addCoach(coach);
    }


    //根据id删除
    @Override
    public int deleteCoach(String id) {
        return coachDao.deleteCoach(id);
    }


    //修改宿舍信息
    @Override
    public int updateCoach(Coach coach) {
        return coachDao.updateCoach(coach);
    }

    //根据ID查询
    @Override
    public Coach findCoachById (String id){
        Coach d = coachDao.findCoachById(id);
        return  d;
    }

    //查询所有
    @Override
    public List<Coach> getAll(){
        List<Coach> adminList = coachDao.getAll();
        return adminList;
    }

    //按照条件查询
    @Override
    public List<Coach> queryFilter(Map mp){
        List<Coach> adminList = coachDao.queryFilter(mp);
        return adminList;
    }
}
